package com.wdb007.venus.svc.biz.coupon;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.wdb.exchange.constant.EventEnum;
import com.wdb.exchange.model.CouponBookShelfInfoModel;
import com.wdb007.venus.svc.biz.manager.IntersystemCommunicationManager;
import com.wdb007.venus.svc.biz.utils.DateFormatUtil;
import com.wdb007.venus.svc.dal.coupon.dao.CouponDAO;
import com.wdb007.venus.svc.dal.coupon.dao.CouponLogDAO;
import com.wdb007.venus.svc.dal.coupon.dao.CouponRuleDAO;
import com.wdb007.venus.svc.dal.coupon.model.CouponBookShelfInfoDO;
import com.wdb007.venus.svc.dal.coupon.model.CouponDO;
import com.wdb007.venus.svc.dal.coupon.model.CouponInfoDO;
import com.wdb007.venus.svc.dal.coupon.model.CouponLogDO;
import com.wdb007.venus.svc.dal.coupon.model.CouponQueryDO;
import com.wdb007.venus.svc.dal.coupon.model.CouponRuleDO;
import com.wdb007.venus.svc.facade.CouponService;
import com.wdb007.venus.svc.facade.UserCostFlagService;
import com.wdb007.venus.svc.facade.UserService;
import com.wdb007.venus.svc.facade.VipMemberService;
import com.wdb007.venus.svc.facade.account.model.UserCostFlagDTO;
import com.wdb007.venus.svc.facade.account.model.VipMemberLogDTO;
import com.wdb007.venus.svc.facade.constant.BizConstants;
import com.wdb007.venus.svc.facade.constant.RspCodeEnum;
import com.wdb007.venus.svc.facade.coupon.model.CouponInfo;
import com.wdb007.venus.svc.facade.exception.BizException;

@Service("couponService")
public class CouponServiceImpl implements CouponService {

	private Logger logger = LoggerFactory.getLogger(CouponServiceImpl.class);
	
	@Autowired
	private CouponDAO couponDAO;
	@Autowired
	private CouponLogDAO couponLogDAO;
	
	@Autowired
	private CouponRuleDAO couponRuleDAO;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private VipMemberService vipMemberService;
	
	@Autowired
	private UserCostFlagService userCostFlagService;
	
	@Autowired
	private IntersystemCommunicationManager intersystemCommunicationManager;
	private Map<Integer, String> eventMap = new HashMap<Integer, String>();
	{
		eventMap.put(BizConstants.COUPON_EVENT_TOOK, "TOOK");
		eventMap.put(BizConstants.COUPON_EVENT_USED, "USED");
		eventMap.put(BizConstants.COUPON_EVENT_EXPIRED, "EXPIRED");
		eventMap.put(BizConstants.COUPON_EVENT_LOCK, "LOCK");
	}
	@Override
	public List<CouponInfo> getCouponList(String oper, Long userId) throws BizException{
		
		CouponQueryDO query = new CouponQueryDO();
		query.setOper(oper);
		query.setUserId(userId);
		List<CouponInfoDO> list = couponDAO.getCouponList(query);
		if(ObjectUtils.isEmpty(list))
			throw new BizException(RspCodeEnum.QUERY_LIST_IS_NULL);
		
		List<CouponInfo> infoList = new ArrayList<CouponInfo>(list.size());
		for(CouponInfoDO infoDO : list) {
			CouponInfo info = new CouponInfo();
			//1.设置优惠券ID
			info.setCoupon_id(infoDO.getCouponId());
			//2.设置优惠券类型
			if(1 == infoDO.getType()) {
				info.setCoupon_type("满减券");
			}else if(2 == infoDO.getType()) {
				info.setCoupon_type("满赠券");
			}else if(3 == infoDO.getType()) {
				info.setCoupon_type("折扣券");
			}else if(4 == infoDO.getType()) {
				info.setCoupon_type("绘本赠送券");
			}
			//3.设置优惠券减幅
			if(1 == infoDO.getType() || 2 == infoDO.getType()) {
				info.setCoupon_cash("¥"+infoDO.getDiscount());
			}else if(3 == infoDO.getType()){
				if(null == infoDO.getDiscount() || 0 == infoDO.getDiscount()) {
					info.setCoupon_cash("免费");
				}else {
					if(infoDO.getDiscount() % 10 == 0) {
						info.setCoupon_cash((infoDO.getDiscount()/10) + "折");
					}else {
						BigDecimal bd = new BigDecimal(infoDO.getDiscount()+"");
						info.setCoupon_cash(bd.divide(new BigDecimal("10")).setScale(1).toString() + "折");
					}
				}
				
			}else {
				info.setCoupon_cash("免费");
			}
			//4.设置标题
			info.setCoupon_desc(infoDO.getTitle());
			//5.设置说明
			if(null == infoDO.getMemo()) {
				info.setCoupon_info("");
			}else {
				info.setCoupon_info(infoDO.getMemo());
			}
			
			//6.设置满足条件
			if(1 == infoDO.getType()) {
				info.setCoupon_condition("满¥"+ infoDO.getNeedAmount() +"使用");
			}else if(2 == infoDO.getType()) {
				info.setCoupon_condition("充值¥"+ infoDO.getNeedAmount() +"使用");
			}else if(3 == infoDO.getType()) {
				if(1 == infoDO.getOffType()) { //月卡使用
					info.setCoupon_condition("购买月卡使用");
				}else if(2 == infoDO.getOffType()){ //年卡使用
					info.setCoupon_condition("购买年卡使用");
				}
			}else if(4 == infoDO.getType()) {
				info.setCoupon_condition("在规定书柜地点使用");
			}
			Date validStartTime = infoDO.getValidStartTime();
			Date validEndTime = infoDO.getValidEndTime();
			//7.设置有效期限
			if(null == validEndTime) {
				info.setOver_time("长期有效");
			}else {
				info.setOver_time(DateFormatUtil.formatDate4(validStartTime) + "至" + DateFormatUtil.formatDate4(validEndTime));
				int day = DateFormatUtil.getDays(new Date(), validEndTime);
				//8.设置是否快到期
				if(day < 3) {
					info.setIsNear(1);
				}
			}
			
			infoList.add(info);
		}
		return infoList;
	}

	@Override
	public List<CouponInfo> getChargeCouponList(String oper, Long userid, String amount, String bookcaseId) throws BizException{
		CouponQueryDO query = new CouponQueryDO();
		query.setOper(oper);
		query.setUserId(userid);
		if(ObjectUtils.isEmpty(amount)) {
			query.setAmount(0);
		}else {
			query.setAmount(Integer.valueOf(amount));
		}
		query.setBookshelfCode(bookcaseId);
		List<CouponInfoDO> list = couponDAO.getChargeCouponList(query);
		if(ObjectUtils.isEmpty(list))
			throw new BizException(RspCodeEnum.QUERY_LIST_IS_NULL);
		List<CouponInfo> infoList = new ArrayList<CouponInfo>(list.size());
		for(CouponInfoDO infoDO : list) {
			if(!ObjectUtils.isEmpty(bookcaseId)) {
				if(infoDO.getAvailable() == 0) {
					continue;
				}
			}
			CouponInfo info = new CouponInfo();
			//1.设置优惠券ID
			info.setCoupon_id(infoDO.getCouponId());
			//2.设置优惠券类型
			if(1 == infoDO.getType()) {
				info.setCoupon_type("满减券");
			}else if(2 == infoDO.getType()) {
				info.setCoupon_type("满赠券");
			}else if(3 == infoDO.getType()) {
				if(1 == infoDO.getOffType()) { //月卡使用
					info.setCoupon_type("月卡折扣券");
				}else {
					info.setCoupon_type("年卡折扣券");
				}
				
			}else if(4 == infoDO.getType()) {
				info.setCoupon_type("绘本赠送券");
			}
			//3.设置优惠券减幅
			if(1 == infoDO.getType()) {
				info.setCoupon_cash("满" + infoDO.getNeedAmount() + "元减" + infoDO.getDiscount() + "元");
			}else if(2 == infoDO.getType()){
				info.setCoupon_cash("满" + infoDO.getNeedAmount() + "元赠" + infoDO.getDiscount() + "元");
			}else if(3 == infoDO.getType()){
				if(null == infoDO.getDiscount() || 0 == infoDO.getDiscount()) {
					info.setCoupon_cash("免费");
				}else {
					if(infoDO.getDiscount() % 10 == 0) {
						info.setCoupon_cash((infoDO.getDiscount()/10) + "折");
					}else {
						BigDecimal bd = new BigDecimal(infoDO.getDiscount()+"");
						info.setCoupon_cash(bd.divide(new BigDecimal("10")).setScale(1).toString() + "折");
					}
				}
				
			}else if(4 == infoDO.getType()) {
				info.setCoupon_cash("免费");
			}
			//4.设置标题
			info.setCoupon_desc(infoDO.getTitle());
			//5.设置说明
			if(null == infoDO.getMemo()) {
				info.setCoupon_info("");
			}else {
				info.setCoupon_info(infoDO.getMemo());
			}
			//6.设置满足条件
			if(1 == infoDO.getType()) {
				info.setCoupon_condition("满¥"+ infoDO.getNeedAmount() +"使用");
			}else if(2 == infoDO.getType()) {
				info.setCoupon_condition("充值¥"+ infoDO.getNeedAmount() +"使用");
			}else if(3 == infoDO.getType()) {
				if(1 == infoDO.getOffType()) { //月卡使用
					info.setCoupon_condition("月卡");
				}else if(2 == infoDO.getOffType()){ //年卡使用
					info.setCoupon_condition("年卡");
				}
			}else if(4 == infoDO.getType()) {
				info.setCoupon_condition("在规定书柜地点使用");
			}
			Date validStartTime = infoDO.getValidStartTime();
			Date validEndTime = infoDO.getValidEndTime();
			//7.设置有效期限
			if(null == validEndTime) {
				info.setOver_time("长期有效");
			}else {
				info.setOver_time(DateFormatUtil.formatDate4(validStartTime) + "至" + DateFormatUtil.formatDate4(validEndTime));
				int day = DateFormatUtil.getDays(new Date(), validEndTime);
				//8.设置是否快到期
				if(day < 3) {
					info.setIsNear(1);
				}
			}
			//8.设置是否可用
			info.setAvaiable(infoDO.getAvailable());
			infoList.add(info);
		}
		
		return infoList;
	}

	@Override
	public CouponInfo getCoupon(String oper, Long userid, String amount) throws BizException {
		CouponQueryDO query = new CouponQueryDO();
		query.setOper(oper);
		query.setUserId(userid);
		if("3".equals(oper)) {
			query.setBookshelfCode(amount);
		}else {
			query.setAmount(Integer.valueOf(amount));
		}
		
		
		CouponInfoDO infoDO = couponDAO.getCoupon(query);
		if(null != infoDO) {
			CouponInfo info = new CouponInfo();
			info.setCoupon_id(infoDO.getCouponId());
			info.setCoupon_type(infoDO.getType()+"");
			if(1 == infoDO.getType()) {
				info.setCoupon_desc("满减券");
			}else if(2 == infoDO.getType()) {
				info.setCoupon_desc("满赠券");
			}else if(3 == infoDO.getType()) {
				if(1 == infoDO.getOffType()) { //月卡使用
					info.setCoupon_desc("月卡折扣券");
				}else if(2 == infoDO.getOffType()){ //年卡使用
					info.setCoupon_desc("年卡折扣券");
				}
			}else if(4 == infoDO.getType()) {
				info.setCoupon_desc("绘本券");
			}
			info.setCoupon_cash(infoDO.getDiscount()+"");
			info.setCoupon_condition(infoDO.getNeedAmount()+"");
			return info;
		}
		return null;
	}

	@Override
	public CouponInfo getCouponById(String couponId) throws BizException {
		
		CouponInfoDO infoDO = couponDAO.getCouponById(couponId);
		if(null != infoDO) {
			CouponInfo info = new CouponInfo();
			info.setCoupon_id(infoDO.getCouponId());
			info.setCoupon_type(infoDO.getType()+"");
			if(1 == infoDO.getType()) {
				info.setCoupon_desc("满减券");
			}else if(2 == infoDO.getType()) {
				info.setCoupon_desc("满赠券");
			}else if(3 == infoDO.getType()) {
				if(1 == infoDO.getOffType()) { //月卡使用
					info.setCoupon_desc("月卡折扣券");
				}else if(2 == infoDO.getOffType()){ //年卡使用
					info.setCoupon_desc("年卡折扣券");
				}
			}else if(4 == infoDO.getType()){
				info.setCoupon_desc("绘本劵");
			}
			info.setCoupon_cash(infoDO.getDiscount()+"");
			info.setCoupon_condition(infoDO.getNeedAmount()+"");
			return info;
		}
		return null;
	}

	@Override
	public int updateCoupon(String couponId, Integer status, Long userChargeLogId) throws BizException {
		logger.info("updateCoupon info: couponId[{}], status[{}], userChargeLogId[{}]", couponId, status, userChargeLogId);
		CouponDO couponDO = couponDAO.get(couponId);
		if(null == couponDO) {
			return 0;
		}
		couponDO.setStatus(status);
		couponDO.setUserChargeLogId(userChargeLogId);
		couponDAO.update(couponDO);
		
		CouponLogDO couponLogDO = new CouponLogDO();
		couponLogDO.setCouponId(couponId);
		couponLogDO.setEvent(eventMap.get(status));
		couponLogDO.setCreateTime(new Date());
		couponLogDAO.insert(couponLogDO);
		return 1;
	}

	@Override
	public CouponInfo draw(Long userId, String couponRuleId) throws BizException {
		
		CouponRuleDO couponRuleDO = couponRuleDAO.get(couponRuleId);
		//1.判断该批优惠券是否已下线
		if(2 == couponRuleDO.getStatus()) {
			throw new BizException(RspCodeEnum.COUPON_HAS_OUTLINE);
		}
		
		//2.根据couponId 来查看获取的上限是否已经达到
		Integer limit = couponRuleDO.getUpLimit();
		Integer perLimit = couponRuleDO.getPerLimit();
		if(0 != limit) { //判断该批优惠券总共被领取了多少张
			int drawCount = couponDAO.countByDraw(couponRuleId);
			if(limit <= drawCount) {
				throw new BizException(RspCodeEnum.COUPON_DRAW_UP_LIMIT);
			}
		}
		
		//3.根据coupon_rule中的per_limit判断该用户是否已经领取了该批次优惠券，是否可以继续领取
		CouponDO query = new CouponDO();
		query.setCouponRuleId(couponRuleId);
		query.setUserInfoId(userId);
		int userDrawCount = couponDAO.countByRuleAndUser(query );
		if(null != perLimit && 0 != perLimit) {
			if(perLimit <= userDrawCount) {
				throw new BizException(RspCodeEnum.COUPON_DRAW_PER_LIMIT);
			}
		}
		
		//4.进行领取
		CouponDO coupon = new CouponDO();
		coupon.setCouponRuleId(couponRuleId);
		coupon.setCreateTime(new Date());
		coupon.setStatus(BizConstants.COUPON_EVENT_TOOK);
		coupon.setUserInfoId(userId);
		if(couponRuleDO.getStartDate() != null && couponRuleDO.getStartDate().compareTo(new Date()) > 0) {
			coupon.setValidStartTime(couponRuleDO.getStartDate());
		}else {
			coupon.setValidStartTime(new Date());
		}
		Integer validDays = couponRuleDO.getValidDays();
		Date validEndTime = null;
		if(null != validDays && 0 != validDays) {
			validEndTime = DateFormatUtil.getSomeDay(coupon.getValidStartTime(), validDays);
		}else {
			validEndTime = couponRuleDO.getEndDate();
		}
		coupon.setValidEndTime(validEndTime);
		couponDAO.insert(coupon);
		CouponLogDO couponLogDO = new CouponLogDO();
		couponLogDO.setCouponId(coupon.getId());
		couponLogDO.setCreateTime(new Date());
		couponLogDO.setEvent(eventMap.get(BizConstants.COUPON_EVENT_TOOK));
		couponLogDAO.insert(couponLogDO);
		
		//给rabbit发送消息
		intersystemCommunicationManager.publishEvent(userId, EventEnum.COUPON_SCANCODE_GOT);
				
		CouponInfo info = new CouponInfo();
		info.setCoupon_id(coupon.getId());
		if(null == validEndTime) {
			info.setOver_time("长期有效");
		}else {
			info.setOver_time(DateFormatUtil.formatDate4(coupon.getValidStartTime()) + "至" + DateFormatUtil.formatDate4(validEndTime));	
		}
		
		if(1 == couponRuleDO.getType()) {
			info.setCoupon_type("满减券");
			info.setCoupon_cash("-" + couponRuleDO.getDiscount());
			info.setCoupon_condition("满¥"+ couponRuleDO.getNeedAmount() +"使用");
		}else if(2 == couponRuleDO.getType()) {
			info.setCoupon_type("满赠券");
			info.setCoupon_cash("+" + couponRuleDO.getDiscount());
			info.setCoupon_condition("充值¥"+ couponRuleDO.getNeedAmount() +"使用");
		}else if(3 == couponRuleDO.getType()) {
			if(1 == couponRuleDO.getOffType()) { //月卡使用
				info.setCoupon_type("月卡折扣券");
				info.setCoupon_condition("购买月卡使用");
			}else if(2 == couponRuleDO.getOffType()){ //年卡使用
				info.setCoupon_type("年卡折扣券");
				info.setCoupon_condition("购买年卡使用");
			}
			if(couponRuleDO.getDiscount() % 10 == 0) {
				info.setCoupon_cash((couponRuleDO.getDiscount()/10) + "折");
			}else {
				BigDecimal bd = new BigDecimal(couponRuleDO.getDiscount()+"");
				info.setCoupon_cash(bd.divide(new BigDecimal("10")).setScale(1).toString() + "折");
			}
		}else if(4 == couponRuleDO.getType()) {
			info.setCoupon_type("绘本券");
			info.setCoupon_cash("免费");
			info.setCoupon_condition("在规定书柜地点使用");
		}
		info.setCoupon_desc(couponRuleDO.getTitle());
		return info;
	}

	@Override
	public CouponInfo draw(Long userId, String couponRuleId, String uid) throws BizException {
		
		CouponRuleDO couponRuleDO = couponRuleDAO.get(couponRuleId);
		if(5 != couponRuleDO.getType()) {
			return draw(userId, couponRuleId);
		}
		//1.判断该批优惠券是否已下线
		if(2 == couponRuleDO.getStatus()) {
			throw new BizException(RspCodeEnum.COUPON_HAS_OUTLINE);
		}
		
		//2.根据couponId 来查看获取的上限是否已经达到
		Integer limit = couponRuleDO.getUpLimit();
		Integer perLimit = couponRuleDO.getPerLimit();
		if(0 != limit) { //判断该批优惠券总共被领取了多少张
			int drawCount = couponDAO.countByDraw(couponRuleId);
			if(limit <= drawCount) {
				throw new BizException(RspCodeEnum.COUPON_DRAW_UP_LIMIT);
			}
		}
		
		//3.根据coupon_rule中的per_limit判断该用户是否已经领取了该批次优惠券，是否可以继续领取
		CouponDO query = new CouponDO();
		query.setCouponRuleId(couponRuleId);
		query.setUserInfoId(userId);
		int userDrawCount = couponDAO.countByRuleAndUser(query );
		if(null != perLimit && 0 != perLimit) {
			if(perLimit <= userDrawCount) {
				throw new BizException(RspCodeEnum.COUPON_DRAW_PER_LIMIT);
			}
		}
		//4. 进行领取并消费
		CouponDO coupon = new CouponDO();
		coupon.setCouponRuleId(couponRuleId);
		coupon.setCreateTime(new Date());
		coupon.setStatus(BizConstants.COUPON_EVENT_TOOK);
		coupon.setUserInfoId(userId);
		couponDAO.insert(coupon);
		
		CouponLogDO  couponLogDO = new CouponLogDO();
		couponLogDO.setCouponId(coupon.getId());
		couponLogDO.setCreateTime(new Date());
		couponLogDO.setEvent(eventMap.get(BizConstants.COUPON_EVENT_TOOK));
		couponLogDAO.insert(couponLogDO);
		
		String vipEndDateStr = useSpreadCoupon(userId, coupon.getId(), couponRuleDO.getValidDays());
		
		//给rabbit发送消息
		intersystemCommunicationManager.publishScanAndDrawCouponEvent(Long.valueOf(uid), couponRuleId, EventEnum.COUPON_SCANCODE_GOT);
		//组装返回值.
		CouponInfo info = new CouponInfo();
		info.setCoupon_type("地推券");
		info.setCoupon_cash(couponRuleDO.getValidDays()+"天");
		info.setCoupon_desc("该会员月卡截止日期: " + vipEndDateStr +"");
		info.setOver_time("长期有效");
		info.setCoupon_id(coupon.getId());
		return info;
	}
	
	
	
	private String useSpreadCoupon(Long userId, String couponId, Integer days) throws BizException{
		//更新用户月卡信息
		VipMemberLogDTO vipMemberLogDTO = new VipMemberLogDTO();
		vipMemberLogDTO.setChargeType(BizConstants.VIP_CHARGE_TYPE_30);
		vipMemberLogDTO.setDays(days);
		vipMemberLogDTO.setUserId(userId);
		vipMemberLogDTO.setVipType(1);
		String vipEndDateStr = vipMemberService.saveVipMember(vipMemberLogDTO);
		//更新用户扣费标准信息
		UserCostFlagDTO userCostFlagDTO = new UserCostFlagDTO();
		userCostFlagDTO.setDays(days);
		userCostFlagDTO.setFlag(3);
		userCostFlagDTO.setUserId(userId);
		userCostFlagService.saveUserCostFlag(userCostFlagDTO);
		
		updateCoupon(couponId, BizConstants.COUPON_EVENT_USED, null);
		return vipEndDateStr;
	}
	@Override
	public CouponInfo getCouponRuleInfo(String couponRuleId) throws BizException {
		CouponRuleDO couponRuleDO = couponRuleDAO.get(couponRuleId);
		if(null == couponRuleDO) {
			throw new BizException(RspCodeEnum.OBJ_NOT_FOUND.setRspMessage("优惠券"));
		}
		CouponInfo info = new CouponInfo();
		
		Integer validDays = couponRuleDO.getValidDays();
		if(null != validDays && 0 != validDays) {
			info.setOver_time("领取后"+validDays+"日有效");
		}else {
			if(null == couponRuleDO.getEndDate()) {
				info.setOver_time("长期有效");
			}else {
				info.setOver_time(DateFormatUtil.formatDate4(couponRuleDO.getStartDate()) + "至" + DateFormatUtil.formatDate4(couponRuleDO.getEndDate()));
			}
		}
		info.setCoupon_desc(couponRuleDO.getTitle());
		if(1 == couponRuleDO.getType()) {
			info.setCoupon_type("满减券");
			info.setCoupon_cash("¥" + couponRuleDO.getDiscount());
			info.setCoupon_condition("满¥"+ couponRuleDO.getNeedAmount() +"使用");
		}else if(2 == couponRuleDO.getType()) {
			info.setCoupon_type("满赠券");
			info.setCoupon_cash("¥" + couponRuleDO.getDiscount());
			info.setCoupon_condition("充值¥"+ couponRuleDO.getNeedAmount() +"使用");
		}else if(3 == couponRuleDO.getType()) {
			if(1 == couponRuleDO.getOffType()) { //月卡使用
				info.setCoupon_type("月卡折扣券");
				info.setCoupon_condition("购买月卡使用");
			}else if(2 == couponRuleDO.getOffType()){ //年卡使用
				info.setCoupon_type("年卡折扣券");
				info.setCoupon_condition("购买年卡使用");
			}
			if(null == couponRuleDO.getDiscount() || couponRuleDO.getDiscount() == 0) {
				info.setCoupon_cash("免费");
			}else if(couponRuleDO.getDiscount() % 10 == 0) {
				info.setCoupon_cash((couponRuleDO.getDiscount()/10) + "折");
			}else {
				BigDecimal bd = new BigDecimal(couponRuleDO.getDiscount()+"");
				info.setCoupon_cash(bd.divide(new BigDecimal("10")).setScale(1).toString() + "折");
			}
		}else if(4 == couponRuleDO.getType()) {
			info.setCoupon_type("赠书券");
			info.setCoupon_cash("免费");
			info.setCoupon_condition("在规定书柜地点使用");
		}else if(5 == couponRuleDO.getType()) {
			info.setCoupon_type("地推券");
			info.setCoupon_cash(couponRuleDO.getValidDays()+"天");
			info.setOver_time("长期有效");
			info.setCoupon_desc(couponRuleDO.getMemo());
			info.setCoupon_condition("");
		}
		return info;
	}

	@Override
	public void scheduleUpdateCouponLock() {
		//1.查找占用的
		logger.info("查找占用状态的优惠券列表");
		List<String> lockedList = couponDAO.getLockedList();
		if(!CollectionUtils.isEmpty(lockedList)) {
			for(String couponId : lockedList) {
				try {
					updateCoupon(couponId, BizConstants.COUPON_EVENT_TOOK, null);
				} catch (BizException e) {
					logger.error("定时更新优惠券占用状态报错：couponId:{}", couponId);
				}
			}
		}
		//2.查找优惠券是否到期
		logger.info("查找有效期失效的优惠券列表");
		List<String> expiredList = couponDAO.getExpireList();
		if(!CollectionUtils.isEmpty(expiredList)) {
			for(String couponId: expiredList) {
				try {
					updateCoupon(couponId, BizConstants.COUPON_EVENT_EXPIRED, null);
				}catch(BizException e) {
					logger.error("定时更新优惠券过期状态报错：couponId:{}", couponId);
				}
			}
		}
	}

	@Override
	public void isNewUser(String coupon_rule_id, String mobile_no) throws BizException {
		
		CouponRuleDO couponRuleDO = couponRuleDAO.get(coupon_rule_id);
		if(null == couponRuleDO) {
			throw new BizException(RspCodeEnum.OBJ_NOT_FOUND.setRspMessage("优惠券"));
		}
		if(1 == couponRuleDO.getForNewUser()) {
			boolean isNew = userService.isNewUser(mobile_no);
			if(!isNew) {
				throw new BizException(RspCodeEnum.COUPON_USER_IS_NOT_NEW);
			}
		}
	}

	@Override
	public Map<String, Object> couponBookShelfInfo(String couponRuleId) {
		List<CouponBookShelfInfoDO> dos = couponRuleDAO.couponBookShelfInfo(couponRuleId);

		List<CouponBookShelfInfoModel> models = new ArrayList<CouponBookShelfInfoModel>();
		try {
			if(!ObjectUtils.isEmpty(dos) && dos.size() > 0) {
				for(CouponBookShelfInfoDO info:dos) {
					CouponBookShelfInfoModel model = new CouponBookShelfInfoModel();
					model.setAddress(info.getAddress());
					model.setArea(info.getArea());
					model.setPolicyRule(info.getWorkingTime());
					models.add(model);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Map<String, Object> detail = new HashMap<>();
		detail.put("couponBookShelfInfo", models);
		logger.info("service couponBookShelfInfo {}",models);
		return detail;
	}

	protected boolean isEmpty(Object obj) {
		if (ObjectUtils.isEmpty(obj) || "null".equals(obj) || "undetined".equals(obj)) {
			return true;
		}
		return false;
	}

	@Override
	public void scanQrCode(String coupon_rule_id, String uid) throws BizException {
		
		intersystemCommunicationManager.publishScanAndDrawCouponEvent(Long.valueOf(uid), coupon_rule_id, EventEnum.COUPON_SCANCODE);
	}

}
